﻿
学生列表页
@*添加按钮*@
<div class="container">
    <div class="row">
        <div class="col-1">
            <button id="addStudent" class="btn btn-primary" type="button" data-bs-toggle="offcanvas" data-bs-target="#addStudentModel" aria-controls="offcanvasWithBothOptions">添加</button>
        </div>
        <div class="col-2">
            <div class="input-group mb-3">
                <input type="text" class="form-control" id="searchWords" placeholder="学生姓名" aria-label="Recipient's username" aria-describedby="searchClass">
                <button class="btn btn-outline-secondary" type="button" id="searchStudent">搜索</button>
            </div>
        </div>
        <div class="col-1">
        </div>
        <div class="col-2">
            <div class="input-group input-group mb-2">
                <label class="input-group-text">年级:</label>
                <select id="findGradeId" class="form-select form-select" aria-label="">
                    <option value="0">全部</option>
                </select>
            </div>
        </div>
        <div class="col-2">
            <div class="input-group input-group mb-2">
                <label class="input-group-text">专业:</label>
                <select id="findDepartmentId" class="form-select form-select" aria-label="">
                    <option value="0">全部</option>
                </select>
            </div>
        </div>
        <div class="col-2">
            <div class="input-group input-group mb-2">
                <label class="input-group-text">班级:</label>
                <select id="findClassId" class="form-select form-select" aria-label="">
                    <option value="0">全部</option>
                </select>
            </div>
        </div>
    </div>
</div>


@*表格*@
<table id="studentTable" class="table table-bordered table-hover">
    <thead>
        <tr>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>



@*添加对话框*@
<div class="offcanvas offcanvas-bottom" data-bs-scroll="true" data-bs-backdrop="false" tabindex="-1" id="addStudentModel" aria-labelledby="offcanvasScrollingLabel">
    <div class="offcanvas-header">
        <h5 class="offcanvas-title" id="offcanvasScrollingLabel">添加学生</h5>
        <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
    </div>
    <div class="offcanvas-body">
        <div class="container text-center">
            <div class="row">
                @*第一列*@
                <div class="col-sm-12 col-md-6 col-lg-3">
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic-addon4">姓名</span>
                        <input type="text" id="studentName" class="form-control" aria-describedby="basic-addon1">
                    </div>

                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic-addon6">学号</span>
                        <input type="text" class="form-control" id="studentNum" aria-describedby="basic-addon1">
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <label class="input-group-text">性别</label>
                        <select id="sex" class="form-select form-select-sm" aria-label="">
                            <option value="1">男</option>
                            <option value="0">女</option>
                        </select>
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic-addon10">民族</span>
                        <input type="text" class="form-control" id="nation" aria-describedby="basic-addon1">
                    </div>
                   
                </div>
                @*第二列*@
                <div class="col-sm-12 col-md-6 col-lg-3">
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic-addon2">年级</span>
                        <select id="gradeId" class="form-select form-select-sm" aria-label="">
                            <option value="0">请选择</option>
                        </select>
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic-addon2">专业</span>
                        <select id="departmentId" class="form-select form-select-sm" aria-label="">
                            <option value="0">请选择</option>
                        </select>
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic-addon2">班级</span>
                        <select id="classEntityId" class="form-select form-select-sm" aria-label="">
                            <option value="0">请选择</option>
                        </select>
                    </div>
                    
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic-addon2">寝室</span>
                        <select id="bedRoomId" class="form-select form-select-sm" aria-label="">
                            <option value="0">请选择</option>
                        </select>
                    </div>

                </div>
                
                
                @*第四列*@
                <div class="col-sm-12 col-md-6 col-lg-3">
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic-addon8">身份证号</span>
                        <input type="text" class="form-control" id="iDNumber" aria-describedby="basic-addon1">
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic-addon9">出生日期</span>
                        <input type="text" class="form-control" id="birthday" aria-describedby="basic-addon1" disabled>
                    </div>
                   
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic-addon11">政治面貌</span>
                        <input type="text" class="form-control" id="political" aria-describedby="basic-addon1">
                    </div>
                    
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic-addon11">住址</span>
                        <input type="text" class="form-control" id="address" aria-describedby="basic-addon1">
                    </div>
                </div>
                @*第五列*@
                <div class="col-sm-12 col-md-6 col-lg-3">
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic-addon11">父亲姓名</span>
                        <input type="text" class="form-control" id="fatherName" aria-describedby="basic-addon1">
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic-addon10">父亲电话</span>
                        <input type="text" class="form-control" id="fatherPhone" aria-describedby="basic-addon1">
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic-addon11">母亲姓名</span>
                        <input type="text" class="form-control" id="motherName" aria-describedby="basic-addon1">
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text" id="basic-addon11">母亲电话</span>
                        <input type="text" class="form-control" id="motherPhone" aria-describedby="basic-addon1">
                    </div>
                    
                    <div class="modal-footer form-control-sm">
                        <button type="button" class="btn btn-primary btn-sm" id="submitAdd">保存</button>
                        <button type="button" class="btn btn-secondary btn-sm" id="clearStudent" data-bs-dismiss="modal">清空</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


@*消息框*@
<div class="toast-container position-fixed top-0 start-50 p-3">
    <div id="liveToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
        <div class="toast-header">
            <strong class="me-auto">消息</strong>
            <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
        </div>
        <div class="toast-body" id="message">
        </div>
    </div>
</div>



@*编辑对话框*@
<div class="offcanvas offcanvas-bottom" data-bs-scroll="true" data-bs-backdrop="false" tabindex="-1" id="editStudentModel" aria-labelledby="offcanvasScrollingLabel">
    <div class="offcanvas-header">
        <h5 class="offcanvas-title" id="offcanvasScrollingLabel">编辑学生</h5>
        <button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
    </div>
    <div class="offcanvas-body">
        <div class="container text-center">
            <div class="row">
                @*第一列*@
                <div class="col-sm-12 col-md-6 col-lg-3">
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text">学生id</span>
                        <input type="text" id="newStudentId" class="form-control" aria-describedby="basic-addon1" disabled>
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text">姓名</span>
                        <input type="text" id="newStudentName" class="form-control" aria-describedby="basic-addon1">
                    </div>

                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text">学号</span>
                        <input type="text" class="form-control" id="newStudentNum" aria-describedby="basic-addon1">
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <label class="input-group-text">性别</label>
                        <select id="newSex" class="form-select form-select-sm" aria-label="">
                            <option value="1">男</option>
                            <option value="0">女</option>
                        </select>
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text">民族</span>
                        <input type="text" class="form-control" id="newNation" aria-describedby="basic-addon1">
                    </div>

                </div>
                @*第二列*@
                <div class="col-sm-12 col-md-6 col-lg-3">
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text">年级</span>
                        <select id="newGradeId" class="form-select form-select-sm" aria-label="">
                            <option value="0">请选择</option>
                        </select>
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text">专业</span>
                        <select id="newDepartmentId" class="form-select form-select-sm" aria-label="">
                            <option value="0">请选择</option>
                        </select>
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text">班级</span>
                        <select id="newClassEntityId" class="form-select form-select-sm" aria-label="">
                            <option value="0">请选择</option>
                        </select>
                    </div>

                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text">寝室</span>
                        <select id="newBedRoomId" class="form-select form-select-sm" aria-label="">
                            <option value="0">请选择</option>
                        </select>
                    </div>

                </div>


                @*第四列*@
                <div class="col-sm-12 col-md-6 col-lg-3">
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text">身份证号</span>
                        <input type="text" class="form-control" id="newIDNumber" aria-describedby="basic-addon1">
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text">出生日期</span>
                        <input type="text" class="form-control" id="newBirthday" aria-describedby="basic-addon1" disabled>
                    </div>

                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text">政治面貌</span>
                        <input type="text" class="form-control" id="newPolitical" aria-describedby="basic-addon1">
                    </div>

                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text">住址</span>
                        <input type="text" class="form-control" id="newAddress" aria-describedby="basic-addon1">
                    </div>
                </div>
                @*第五列*@
                <div class="col-sm-12 col-md-6 col-lg-3">
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text">父亲姓名</span>
                        <input type="text" class="form-control" id="newFatherName" aria-describedby="basic-addon1">
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text">父亲电话</span>
                        <input type="text" class="form-control" id="newFatherPhone" aria-describedby="basic-addon1">
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text">母亲姓名</span>
                        <input type="text" class="form-control" id="newMotherName" aria-describedby="basic-addon1">
                    </div>
                    <div class="input-group input-group-sm mb-2">
                        <span class="input-group-text">母亲电话</span>
                        <input type="text" class="form-control" id="newMotherPhone" aria-describedby="basic-addon1">
                    </div>

                    <div class="modal-footer form-control-sm">
                        <button type="button" class="btn btn-primary btn-sm" id="submitEdit">保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@*确定删除对话框*@
<div class="modal fade" id="sureDel" tabindex="-1" data-bs-backdrop="static" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <h1 class="modal-title fs-5" id="exampleModalLabel">删除班级</h1>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p>确认删除吗?</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-bs-dismiss="modal" id="submitDel">确定</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

@section Scripts{
    <script>
        loadpage()
        let g_rows
        let gradelist
        let deplist
        let classlist
        let bedRoomlist
        getSelectData()
        rendSelects()
        rendTopSelects()
        function loadpage() {
            let s = sessionStorage.getItem('studentlist')

            if (s == [] || s == "" || s == null) {
                $.ajax({
                    url: '/Student/GetStudentList',
                    type: 'get',
                    success: function (data) {
                        parseData(data)
                    },
                    error: function (err) {
                    }
                })
            } else {
                parseStorage(s)
            }

        }

        //解析请求数据并渲染到页面
        function parseData(data) {
            $('#studentTable thead tr').empty()
            $('#studentTable tbody tr').empty()
            let jsd = JSON.stringify(data)

            let head = JSON.parse(data.attr)
            head.push('操作')
            sessionStorage.setItem('studentlist_head', JSON.stringify(head))

            //渲染表头
            for (index in head) {
                $('#studentTable thead tr').append('<th>' + head[index] + '</th>')
            }

            let rows = JSON.parse(data.row)
            g_rows = rows
            $(rows).each(function(index,ele){
                ele.Sex=ele.Sex==1?'男':'女'
                ele.Birthday=ele.Birthday.substr(0,10)
                for(it in ele )
                {
                   if(ele[it]==null){
                    ele[it]=''
                   }
                }
            })
            sessionStorage.setItem('studentlist', JSON.stringify(rows))

            //渲染表格
            $(rows).each(function (index, ele) {
                $('#studentTable tbody').append('<tr></tr>')
                for (key in ele) {
                    $('#studentTable tbody tr:last').append('<td>' + ele[key] + '</>')
                }

                $('#studentTable tbody tr:last').append('<td>' +
                    '<button class="editBtn" onclick="edit(this)" data-bs-toggle="offcanvas" data-bs-target="#editStudentModel">编辑</button>' +
                    '<button class="delBtn" onclick="del(this)" data-bs-toggle="modal" data-bs-target="#sureDel">删除</button>' +
                    '</td>')
                $('#studentTable tbody tr td:last').attr({ 'studentid': ele.StudentId })
            })

            $('.delBtn').addClass("btn btn-danger btn-sm")
            $('.editBtn').addClass("btn btn-primary btn-sm")
        }

        //解析缓存并加载到表格
        function parseStorage(data) {
            $('#studentTable thead tr').empty()
            $('#studentTable tbody tr').empty()
            let headStorage = sessionStorage.getItem('studentlist_head')
            let head = JSON.parse(headStorage)
            //渲染表头
            for (index in head) {
                $('#studentTable thead tr').append('<th>' + head[index] + '</th>')
            }

            //渲染表格
            let rows = JSON.parse(data)
            $(rows).each(function (index, ele) {
                $('#studentTable tbody').append('<tr></tr>')
                for (key in ele) {
                    $('#studentTable tbody tr:last').append('<td>' + ele[key] + '</>')
                }
                $('#studentTable tbody tr:last').append('<td>' +
                    '<button class="editBtn" onclick="edit(this)" data-bs-toggle="offcanvas" data-bs-target="#editStudentModel">编辑</button>' +
                    '<button class="delBtn" onclick="del(this)" data-bs-toggle="modal" data-bs-target="#sureDel">删除</button>' +
                    '</td>')
                $('#studentTable tbody tr td:last').attr({ 'studentid': ele.StudentId })
            })

            $('.delBtn').addClass("btn btn-danger btn-sm")
            $('.editBtn').addClass("btn btn-primary btn-sm")
        }


        //获取下拉框数据
        function getSelectData() {
            let gradeStor = JSON.parse(sessionStorage.getItem('gradelist'))
            let depStor = JSON.parse(sessionStorage.getItem('deplist'))
            let classStor = JSON.parse(sessionStorage.getItem('classlist'))
            let bedRoomStor = JSON.parse(sessionStorage.getItem('bedRoomlist'))
            if (bedRoomStor == null || gradeStor == null || depStor==null) {
                initSelectData()
            }
            else {
                gradelist=gradeStor
                classlist=classStor
                deplist=depStor
                bedRoomlist = bedRoomStor
            }

        }


        //初始化下拉框数据 存缓存
        function initSelectData() {
            $.ajax({
                url: '/Grade/GetGradeList',
                type: 'get',
                success: function (data) {
                    sessionStorage.setItem('gradelist', data.row)
                    let head = JSON.parse(data.attr)
                    head.push('操作')
                    sessionStorage.setItem('gradelist_head', JSON.stringify(head))
                    gradelist = JSON.parse(data["row"])
                },
                error: function (err) {
                }
            })
            
            $.ajax({
                url: '/Department/GetDepList',
                type: 'get',
                success: function (data) {
                    sessionStorage.setItem('deplist', data.row)
                    let head = JSON.parse(data.attr)
                    head.push('操作')
                    sessionStorage.setItem('deplist_head', JSON.stringify(head))
                    deplist = JSON.parse(data["row"])
                },
                error: function (err) {
                }
            })

            $.ajax({
                url: '/ClassEntity/GetClassList',
                type: 'get',
                success: function (data) {
                    sessionStorage.setItem('classlist', data.row)

                    let head = JSON.parse(data.attr)
                    head.push('操作')
                    sessionStorage.setItem('classlist_head', JSON.stringify(head))
                    classlist = JSON.parse(data['row'])
                },
                error: function (err) {
                }
            })

            $.ajax({
                url: '/BedRoom/GetBedRoomList',
                type: 'get',
                success: function (data) {
                    sessionStorage.setItem('bedRoomlist', data.row)

                    let head = JSON.parse(data.attr)
                    head.push('操作')
                    sessionStorage.setItem('bedRoomlist_head', JSON.stringify(head))
                    bedRoomlist = JSON.parse(data['row'])
                },
                error: function (err) {
                }
            })

        }

        //渲染"添加"对话框中下拉框
        function rendSelects() {
            $('#gradeId').empty()
            $('#gradeId').append("<option value='0'>请选择</option>")
            $(gradelist).each(function (index, ele) {
                $('#gradeId').append("<option value=''></option>")
                $('#gradeId option:last').val(ele.GradeId)
                $('#gradeId option:last').text(ele.GradeName)
            })

            $('#departmentId').empty()
            $('#departmentId').append("<option value='0'>请选择</option>")
            $(deplist).each(function (index, ele) {
                $('#departmentId').append("<option value=''></option>")
                $('#departmentId option:last').val(ele.DepartmentId)
                $('#departmentId option:last').text(ele.DepName)
            })

            $('#bedRoomId').empty()
            $('#bedRoomId').append("<option value='0'>请选择</option>")
            $(bedRoomlist).each(function (index, ele) {
                $('#bedRoomId').append("<option value=''></option>")
                $('#bedRoomId option:last').val(ele.BedRoomId)
                $('#bedRoomId option:last').text(ele.BedRoomName)
            })

        }

        //点击“添加”渲染下接列表
        $('#addStudent').click(function () {
            rendSelects()
        })


        function rendClassSelect(data)
        {
            //“添加”对话框中的班级选择
            $('#classEntityId').empty()
            $('#classEntityId').append("<option value='0'>请选择</option>")
            $(data).each(function (index, ele) {
                $('#classEntityId').append("<option value=''></option>")
                $('#classEntityId option:last').val(ele.ClassEntityId)
                $('#classEntityId option:last').text(ele.ClassName)
            })


            //“编辑”对话框中的班级选择
            $('#newClassEntityId').empty()
            $('#newClassEntityId').append("<option value='0'>请选择</option>")
            $(data).each(function (index, ele) {
                $('#newClassEntityId').append("<option value=''></option>")
                $('#newClassEntityId option:last').val(ele.ClassEntityId)
                $('#newClassEntityId option:last').text(ele.ClassName)
            })

            //“顶部”对话框中的班级选择
            $('#findClassId').empty()
            $('#findClassId').append("<option value='0'>全部</option>")
            $(data).each(function (index, ele) {
                $('#findClassId').append("<option value=''></option>")
                $('#findClassId option:last').val(ele.ClassEntityId)
                $('#findClassId option:last').text(ele.ClassName)
            })
        }

        
        //“添加”对话框中下拉框改变事件
        $('#gradeId').change(function () {
            let gradeId = $('#gradeId').val()
            let departmentId = $('#departmentId').val()
            findClassEntity(gradeId, departmentId)
        })

        $('#departmentId').change(function () {
            let gradeId = $('#gradeId').val()
            let departmentId = $('#departmentId').val()
            findClassEntity(gradeId, departmentId)
        })


        function findClassEntity(gradeId, departmentId) {
            if (gradeId == 0 && departmentId == 0) {
                $.ajax({
                    url: '/ClassEntity/GetClassList',
                    type: 'post',
                    data: {
                        GradeId: gradeId,
                        DepartmentId: departmentId
                    },
                    success: function (data) {
                        let res = JSON.parse(data.row)
                        rendClassSelect(res)
                    },
                    error: {

                    }
                })
                return
            }
            if (gradeId != 0 && departmentId == 0) {
                $.ajax({
                    url: '/ClassEntity/GetClassByGradeId',
                    type: 'post',
                    data: {
                        GradeId: gradeId,
                        DepartmentId: departmentId
                    },
                    success: function (data) {
                        let res = JSON.parse(data.row)
                        rendClassSelect(res)
                    },
                    error: {

                    }
                })
                return
            }

            if (gradeId == 0 && departmentId != 0) {
                $.ajax({
                    url: '/ClassEntity/GetClassByDepartmentId',
                    type: 'post',
                    data: {
                        GradeId: gradeId,
                        DepartmentId: departmentId
                    },
                    success: function (data) {
                        let res = JSON.parse(data.row)
                        rendClassSelect(res)
                    },
                    error: {

                    }
                })
                return
            }

            if (gradeId != 0 && departmentId != 0) {
                $.ajax({
                    url: '/ClassEntity/GetClassByGradeIdAndDepartmentId',
                    type: 'post',
                    data: {
                        GradeId: gradeId,
                        DepartmentId: departmentId
                    },
                    success: function (data) {
                        let res = JSON.parse(data.row)
                        rendClassSelect(res)
                    },
                    error: function (err) {
                    }
                })
                return
            }
        }


        //"身份证号"框失去焦点事件
        $('#iDNumber').blur(function(){
            let IDNumber = $('#iDNumber').val()
            let year = IDNumber.substr(6, 4)
            let month = IDNumber.substr(10, 2)
            let day = IDNumber.substr(12, 2)
            let birthday=year+'-'+month+'-'+day
            $('#birthday').val(birthday)
        })

        //编辑对话框中"身份证号"框失去焦点事件
        $('#newIDNumber').blur(function () {
            let IDNumber = $('#newIDNumber').val()
            let year = IDNumber.substr(6, 4)
            let month = IDNumber.substr(10, 2)
            let day = IDNumber.substr(12, 2)
            let birthday = year + '-' + month + '-' + day
            $('#newBirthday').val(birthday)
        })
        //消息框实例
        const toastLiveExample = document.getElementById('liveToast')
        const toast = new bootstrap.Toast(toastLiveExample)
        //提交添加
        $('#submitAdd').click(function () {
            let StudentName = $('#studentName').val()
            let StudentNum = $('#studentNum').val()
            let Sex = $('#sex').val()
            let ClassEntityId = $('#classEntityId').val()
            let BedRoomId = $('#bedRoomId').val()
            let IDNumber = $('#iDNumber').val()
            let Birthday = $('#birthday').val()
            let Nation = $('#nation').val()
            let Political = $('#political').val()
            let FatherName = $('#fatherName').val()
            let FatherPhone = $('#fatherPhone').val()
            let MotherName = $('#motherName').val()
            let MotherPhone = $('#motherPhone').val()
            let Address = $('#address').val()
            if (StudentName == '' || StudentName == null) {

                $('#message').text('名称不能为空!')
                toast.show()
                return
            }

            if (ClassEntityId <= 0) {
                $('#message').text('班级必选!')
                toast.show()
                return
            }
            if (BedRoomId <= 0) {
                $('#message').text('寝室必选!')
                toast.show()
                return
            }

            $.ajax({
                url: '/Student/AddStudent',
                type: 'post',
                data: {
                    StudentName,
                    StudentNum,
                    Sex,
                    ClassEntityId,
                    BedRoomId,
                    IDNumber,
                    Birthday,
                    Nation,
                    Political,
                    FatherName,
                    FatherPhone,
                    MotherName,
                    MotherPhone,
                    Address
                },
                success: function (data) {
                    if (data == '1') {
                        $('#message').text('添加成功!')
                        toast.show()
                        refreshData()
                        clearData()
                    }
                    if (data == '-1') {
                        $('#message').text('班级名称已存在!')
                        toast.show()
                    }
                },
                error: function (err) {
                }
            })
        })


        //刷新数据
        function refreshData() {
            $('#studentTable thead tr').empty()
            $('#studentTable tbody tr').empty()

            $.ajax({
                url: '/Student/GetStudentList',
                type: 'get',
                success: function (data) {
                    parseData(data)
                },
                error: function (err) {
                }
            })

        }

        //清空添加对话框
        function clearData()
        {
            $('#studentName').val('')
            $('#studentNum').val('')
           
            $('#bedRoomId').val(0)
            $('#iDNumber').val('')
            $('#birthday').val('')
            $('#nation').val('')
            $('#political').val('')
            $('#fatherName').val('')
            $('#fatherPhone').val('')
            $('#motherName').val('')
            $('#motherPhone').val('')
            $('#address').val('')
        }

        //清空按钮
        $('#clearClassEntity').click(function () {
            clearData()
        })


        //第一次加载页面--“编辑”按钮响应事件
        function edit(e) {
            rendEditSelects()
            let studentId = $(e).parent().attr('studentid')
            $.ajax({
                url: '/Student/GetStudentById',
                type: 'post',
                data:{
                    StudentId: studentId
                },
                success: function (data) {
                    let student=data
                    $(student).each(function(index,ele){
                        ele.birthday=ele.birthday.substr(0,10)
                    })
                    $('#newStudentId').val(student.studentId)
                    $('#newStudentId').attr({ 'disabled': 'disabled' })
                    $('#newStudentName').val(student.studentName)
                    $('#newStudentNum').val(student.studentNum)
                    $('#newSex').val(student.sex)
                    $('#newNation').val(student.nation)
                    $('#newGradeId').val(student.gradeId)
                    $('#newDepartmentId').val(student.departmentId)
                    $('#newClassEntityId').val(student.classEntityId)
                    $('#newBedRoomId').val(student.bedRoomId)
                    $('#newIDNumber').val(student.idNumber)
                    $('#newBirthday').val(student.birthday)
                    $('#newPolitical').val(student.political)
                    $('#newAddress').val(student.address)
                    $('#newFatherName').val(student.fatherName)
                    $('#newFatherPhone').val(student.fatherPhone)
                    $('#newMotherName').val(student.motherName)
                    $('#newMotherPhone').val(student.motherPhone)
                },
                error: function (err) {
                }
            })
        }

       

        //渲染"编辑"对话框中下拉框
        function rendEditSelects() {
            $('#newGradeId').empty()
            $('#newGradeId').append("<option value='0'>请选择</option>")
            $(gradelist).each(function (index, ele) {
                $('#newGradeId').append("<option value=''></option>")
                $('#newGradeId option:last').val(ele.GradeId)
                $('#newGradeId option:last').text(ele.GradeName)
            })

            $('#newDepartmentId').empty()
            $('#newDepartmentId').append("<option value='0'>请选择</option>")
            $(deplist).each(function (index, ele) {
                $('#newDepartmentId').append("<option value=''></option>")
                $('#newDepartmentId option:last').val(ele.DepartmentId)
                $('#newDepartmentId option:last').text(ele.DepName)
            })

            $('#newClassEntityId').empty()
            $('#newClassEntityId').append("<option value='0'>请选择</option>")
            $(classlist).each(function (index, ele) {
                $('#newClassEntityId').append("<option value=''></option>")
                $('#newClassEntityId option:last').val(ele.ClassEntityId)
                $('#newClassEntityId option:last').text(ele.ClassName)
            })

            $('#newBedRoomId').empty()
            $('#newBedRoomId').append("<option value='0'>请选择</option>")
            $(bedRoomlist).each(function (index, ele) {
                $('#newBedRoomId').append("<option value=''></option>")
                $('#newBedRoomId option:last').val(ele.BedRoomId)
                $('#newBedRoomId option:last').text(ele.BedRoomName)
            })

        }


        //“编辑”对话框中下拉框改变事件
        $('#newGradeId').change(function () {
            let gradeId = $('#newGradeId').val()
            let departmentId = $('#newDepartmentId').val()
            findClassEntity(gradeId, departmentId)
        })

        $('#newDepartmentId').change(function () {
            let gradeId = $('#newGradeId').val()
            let departmentId = $('#newDepartmentId').val()
            findClassEntity(gradeId, departmentId)
        })


        
        //编辑对话框中的“保存”按钮点击事件响应
        $('#submitEdit').click(function (index) {
            let StudentId = $('#newStudentId').val()
            let StudentName = $('#newStudentName').val()
            let StudentNum = $('#newStudentNum').val()
            let Sex = $('#newSex').val()
            let Nation = $('#newNation').val()
            let ClassEntityId = $('#newClassEntityId').val()
            let BedRoomId = $('#newBedRoomId').val()
            let IDNumber = $('#newIDNumber').val()
            let Birthday = $('#newBirthday').val()
            let Political = $('#newPolitical').val()
            let Address = $('#newAddress').val()
            let FatherName = $('#newFatherName').val()
            let FatherPhone = $('#newFatherPhone').val()
            let MotherName = $('#newMotherName').val()
            let MotherPhone = $('#newMotherPhone').val()

            if (StudentName == '' || StudentName==null) {
                //提示名称不能为空
                $('#message').text('名称不能为空!')
                toast.show()
                return
            }

            $.ajax({
                url: '/Student/EditStudent',
                type: 'post',
                data: {
                    StudentId,
                    StudentName,
                    StudentNum,
                    Sex,
                    Nation,
                    ClassEntityId,
                    BedRoomId,
                    IDNumber,
                    Birthday,
                    Political,
                    Address,
                    FatherName,
                    FatherPhone,
                    MotherName,
                    MotherPhone
                },
                success: function (data) {
                    if (data == 1) {
                        $('#message').text('修改成功!')
                        toast.show()
                    }
                    //刷新表格
                    refreshData()
                    //关闭对话框
                    // ...
                },
                error: {

                }
            })
        })


        //"删除"按钮响应事件
        let studentId
        function del(e) {
            studentId = $(e).parent().attr('studentId')
            console.log(studentId)
        }


        //"删除"对话框--"确定"按钮响应事件
        $('#submitDel').click(function () {
            let delId = studentId;
            if (delId <= 0) {
                return
            }

            $.ajax({
                url: '/Student/DelStudent',
                type: 'post',
                data: {
                    StudentId: delId
                },
                success: function (data) {
                    if (data == 1) {
                        $('#message').text('删除成功!')
                        toast.show()
                    }
                    else {
                        $('#message').text('删除失败!')
                        toast.show()
                    }
                    //刷新表格
                    refreshData()
                    //关闭对话框
                    // ...
                },
                error: {

                }
            })
        })


        function rendTable(data) {
            $('#studentTable tbody').empty()
            //渲染表格
            if (data == null) {
                return
            }
            $(data).each(function(index,ele){
                ele.Birthday=ele.Birthday.substr(0,10)
                ele.Sex=ele.Sex==1?'男':'女'
                for(it in ele){
                    if(ele[it]==null){
                        ele[it]=''
                    }
                }
            })

            $(data).each(function (index, ele) {
                $('#studentTable tbody').append('<tr></tr>')
                for (key in ele) {
                    $('#studentTable tbody tr:last').append('<td>' + ele[key] + '</>')
                }

                $('#studentTable tbody tr:last').append('<td>' +
                    '<button class="editBtn" onclick="edit(this)" data-bs-toggle="offcanvas" data-bs-target="#editStudentModel">编辑</button>' +
                    '<button class="delBtn" onclick="del(this)" data-bs-toggle="modal" data-bs-target="#sureDel">删除</button>' +
                    '</td>')
                $('#studentTable tbody tr td:last').attr({ 'studentid': ele.StudentId })
            })

            $('.delBtn').addClass("btn btn-danger btn-sm")
            $('.editBtn').addClass("btn btn-primary btn-sm")
        }

        //搜索按钮
        $('#searchStudent').click(function () {
            let key = $('#searchWords').val()
            if (key == null || key == '') {
                return
            }
            $.ajax({
                url: '/Student/SearchStudent',
                type: 'post',
                data: {
                    StudentName: key
                },
                success: function (data) {
                    let res = JSON.parse(data.row)
                    rendTable(res)
                },
                error: {

                }
            })
        })


        //渲染"顶部"下拉框
        function rendTopSelects() {
            $('#findGradeId').empty()
            $('#findGradeId').append("<option value='0'>全部</option>")
            $(gradelist).each(function (index, ele) {
                $('#findGradeId').append("<option value=''></option>")
                $('#findGradeId option:last').val(ele.GradeId)
                $('#findGradeId option:last').text(ele.GradeName)
            })

            $('#findDepartmentId').empty()
            $('#findDepartmentId').append("<option value='0'>全部</option>")
            $(deplist).each(function (index, ele) {
                $('#findDepartmentId').append("<option value=''></option>")
                $('#findDepartmentId option:last').val(ele.DepartmentId)
                $('#findDepartmentId option:last').text(ele.DepName)
            })

           

        }

        //"顶部"下拉框改变事件
        $('#findGradeId').change(function () {
            let gradeId = $('#findGradeId').val()
            let departmentId = $('#findDepartmentId').val()
            findClassEntity(gradeId, departmentId)
            let classEntityId = $('#findClassId').val()
            
            

            findStudent(gradeId, departmentId, classEntityId)
        })
        $('#findDepartmentId').change(function () {
            let gradeId = $('#findGradeId').val()
            let departmentId = $('#findDepartmentId').val()
            findClassEntity(gradeId, departmentId)
            let classEntityId = $('#findClassId').val()
            
           
            findStudent(gradeId, departmentId, classEntityId)
        })
        $('#findClassId').change(function () {
            let gradeId = $('#findGradeId').val()
            let departmentId = $('#findDepartmentId').val()
            let classEntityId = $('#findClassId').val()
            

            findStudent(gradeId, departmentId, classEntityId)
        })

        function findStudent(gradeId, departmentId, classEntityId) {
              if(gradeId==0&&departmentId==0&&classEntityId==0){
                    $.ajax({
                        url: '/Student/GetStudentList',
                        type: 'get',
                        success: function (data) {
                            parseData(data)
                        },
                        error: function (err) {
                        }
                    })
              } 

              if(gradeId!=0&&departmentId==0&&classEntityId==0){
                    $.ajax({
                        url: '/Student/GetStudentByGradeId',
                        type: 'post',
                        data: {
                            GradeId: gradeId
                        },
                        success: function (data) {
                            rendTable(JSON.parse(data))
                        },
                        error: function (err) {
                        }
                    })
              }


            if (gradeId != 0 && departmentId != 0 && classEntityId == 0) {
                $.ajax({
                    url: '/Student/GetStudentByGradeIdAndDepartmentId',
                    type: 'post',
                    data: {
                        GradeId:gradeId,
                        DepartmentId: departmentId
                    },
                    success: function (data) {
                        rendTable(JSON.parse(data))
                    },
                    error: function (err) {
                    }
                })
            }

            if(gradeId==0&&departmentId!=0&&classEntityId==0){
                $.ajax({
                    url: '/Student/GetStudentByDepartmentId',
                    type: 'post',
                    data: {
                        DepartmentId: departmentId
                    },
                    success: function (data) {
                        rendTable(JSON.parse(data))
                    },
                    error: function (err) {
                    }
                })
            }
          
              if(classEntityId!=0){
                    $.ajax({
                        url: '/Student/GetStudentByClassEntityId',
                        type: 'post',
                        data:{
                            ClassEntityId:classEntityId
                        },
                        success: function (data) {
                        rendTable(JSON.parse(data))
                        },
                        error: function (err) {
                        }
                    })
              }


        }
    </script>
}